--#include "data\config\activityconfig\DailyActivity\SecretTreasureConfig.lua" once
--#include "data\functions\Activity\ActivityGuide.lua" once
--#include "data\functions\Common\RankMgr.lua" once 		--排行榜管理


--进入场景
function SecretTreasureEnterScene(sysarg, reliveType)
	--local maxHp = Actor.getIntProperty(sysarg, PROP_CREATURE_MAXHP)
	--Actor.setUIntProperty(sysarg, PROP_CREATURE_HP, maxHp)
	
	if reliveType and reliveType == 1 then
	else
		local rand = System.getRandomNumber(#SecretTreasureCfg.EnterPos) + 1	
		Actor.enterScene(sysarg,unpack(SecretTreasureCfg.EnterPos[rand]))	
	end
	if not reliveType then
		SecretTreasureRightPanelData(sysarg)
	end

	if not Actor.hasBuffById( sysarg, SecretTreasureCfg.changeBuff ) then
	  	Actor.addBuffById(sysarg, SecretTreasureCfg.changeBuff)	
	end
end	

--怪物死亡
function OnSecretTreasureMonDie(monster, sysarg, monId)
	--[[
	local sceneId = Actor.getSceneId(sysarg)
	if sceneId ~= SecretTreasureCfg.EnterPos[1][1] then
		return
	end
	]]
	local  dVar = System.getDyanmicVar()
	if not dVar.SecretTreasureMonIdx then
		return
	end
	local state = false
	local monIdx
	for idx = 1, SecretTreasureCfg.EveryMonsterCount do
		if dVar.SecretTreasureMonIdx[idx] and dVar.SecretTreasureMonIdx[idx] == monId then
			monIdx = idx
			state = true
			break
		end
	end
	if state == false then
		return
	end
	dVar.SecretTreasureMonIdx[monIdx] = nil
	
	local dropItemId = GetSecretTreasureRewardItem()

	if dropItemId == 0 then
		print("drop item can not be 0")
		return
	end

	if Actor.isDeath(sysarg) or Actor.getSceneId(sysarg) ~= SecretTreasureCfg.EnterPos[1][1] then
		SecretTreasureCreateDropItemMon(sysarg, monIdx, dropItemId)
	else
		SetPropertyHeadItemValue(sysarg, monIdx, dropItemId)
		--[[
		local PropValue = Actor.getUInt64Property(sysarg, PROP_ACTOR_HEAD_ITEM)
		PropValue = System.setInt64UShort(PropValue, monIdx - 1, dropItemId)
		Actor.setUInt64Property(sysarg, PROP_ACTOR_HEAD_ITEM, PropValue)
		]]
		--[[
		local actorId = Actor.getActorId(sysarg)
		gRankMgr_SetRankEx(actorId, RANK_DEFINE_SECRET_TREASURE, monIdx, dropItemId)
		gRankMgr_AddValue(actorId, RANK_DEFINE_SECRET_TREASURE, 1)
		]]

		if not dVar.SecretTreasureActorId then
			dVar.SecretTreasureActorId = {}
		end
		dVar.SecretTreasureActorId[monIdx] = Actor.getActorId(sysarg)
		if not dVar.SecretTreasureActorTime then
			dVar.SecretTreasureActorTime = {}
		end
		dVar.SecretTreasureActorTime[monIdx] = System.getCurrMiniTime() + SecretTreasureCfg.LastTime

		--local playerName = Actor.getName(sysarg)
		local playerName = Lang.ScriptTips.SecretTreasure017

		local nSceneId = SecretTreasureCfg.EnterPos[1][1]
		local hScene = Fuben.getSceneHandleById(nSceneId, 0)
		Fuben.sendMsgToScene(hScene,string.format(Lang.ScriptTips.SecretTreasure010, playerName), ttScreenCenter + ttChatWindow)
		SecretTreasureRightPanelDataAll()
	end
end

--活动开始
function SecretTreasureStar()	
	local cfg = SecretTreasureCfg
	--加经验
	local sysVar = System.getDyanmicVar()
	sysVar.SecretTreasureStart = System.getCurrMiniTime()
	SecretTreasureCreateMonPreTime(1)
	System.broadcastTipmsg(Lang.ScriptTips.SecretTreasure014,ttScreenCenter + ttChatWindow)
	Actor.unregScriptCallback(System.getGlobalNpc(), "OnSecretTreasureExpAdd")
	Actor.regScriptTimer(System.getGlobalNpc(), 0, 0, SecretTreasureCfg.ExpTime*1000, -1, "OnSecretTreasureExpAdd")
end

function OnSecretTreasureExpAdd()
	local sceneId = SecretTreasureCfg.EnterPos[1][1]
	local playerList = LuaHelp.getSceneActorListById(sceneId) or {}
	for i,player in ipairs( playerList ) do
		GiveCommonAwardEx(player, SecretTreasureCfg.ExpAwards, LogIds.SecretTreasureExp, Lang.LogTips.SecretTreasureExp)
	end
end
--回合开始创建怪物
function SecretTreasureCreateMonPreTime(ii)
	for idx = 1, SecretTreasureCfg.EveryMonsterCount do
		SecretTreasureCreateMon(idx)
	end

	local  dVar = System.getDyanmicVar()
	dVar.SecretTreasureActorId = nil
	dVar.SecretTreasureRefeshIdx = ii
	dVar.SecretTreasureRoundEndTime = System.getCurrMiniTime() + SecretTreasureCfg.PreRoundTime

	Actor.unregScriptCallback( System.getGlobalNpc(), "SecretTreasurePreSecondTimeFunc")
	Actor.regScriptTimer(System.getGlobalNpc(), 0, 1000, 1000, SecretTreasureCfg.ActivityTime*1000, "SecretTreasurePreSecondTimeFunc")

	SecretTreasureRightPanelDataAll()
end

--每回合结束清除怪物
function SecretTreasureRoundEndClearMonsters()
	local sceneId = SecretTreasureCfg.EnterPos[1][1]
	local  hScene = Fuben.getSceneHandleById(sceneId, 0)
	local  dVar = System.getDyanmicVar()
	if dVar.SecretTreasureMonIdx then
		for idx = 1, SecretTreasureCfg.EveryMonsterCount do
			if dVar.SecretTreasureMonIdx[idx] then
				Fuben.clearMonster(hScene, dVar.SecretTreasureMonIdx[idx])
			end
		end
		dVar.SecretTreasureMonIdx = nil
	end
	if dVar.SecretTreasureItemMonIdx then
		for idx = 1, SecretTreasureCfg.EveryMonsterCount do
			if dVar.SecretTreasureItemMonIdx[idx] then
				Fuben.clearMonster(hScene, dVar.SecretTreasureItemMonIdx[idx])
			end
		end
		dVar.SecretTreasureItemMonIdx = nil
	end
end

--每回合结束
function SecretTreasureRoundEnd()
	SecretTreasureRoundEndClearMonsters()
	SecretTreasureAwardAll()

	local  dVar = System.getDyanmicVar()
	
	if dVar.SecretTreasureRefeshIdx >= SecretTreasureCfg.MaxCount then
		SecretTreasureEndHandler()
		return
	end
	dVar.SecretTreasureActorTime = nil
	dVar.SecretTreasureActorId = nil
	dVar.SecretTreasureMonIdx = nil
	dVar.SecretTreasureItemMonIdx = nil
	dVar.SecretTreasureRoundEndTime = nil
	dVar.SecretTreasureItemMonItemId = nil
	SecretTreasureCreateMonPreTime(dVar.SecretTreasureRefeshIdx + 1)
	SecretTreasureRightPanelDataAll()
end

--每秒定时器
function SecretTreasurePreSecondTimeFunc()
	local curTime = System.getCurrMiniTime()
	local  dVar = System.getDyanmicVar()
	if not dVar.SecretTreasureRoundEndTime then
		return
	end
	if dVar.SecretTreasureRoundEndTime <= curTime then
		SecretTreasureRoundEnd()
		return
	end
	if not dVar.SecretTreasureActorTime then
		return
	end
	if not dVar.SecretTreasureActorId then
		return 
	end
	local updateClientData = false
	for idx = 1, SecretTreasureCfg.EveryMonsterCount do
		if dVar.SecretTreasureActorTime[idx] and dVar.SecretTreasureActorTime[idx] <= curTime then
			updateClientData = true
			SecretTreasureAward(idx)
		end
	end
	if updateClientData then
		SecretTreasureRightPanelDataAll()
	end
end

--刷怪
function  SecretTreasureCreateMon(idx)
    local  cfg = SecretTreasureCfg
    local  monCount = table.getn(cfg.Monsters)
	local  posCount = table.getn(cfg.MonsterPos)
	local  monIdx = System.getRandomNumber(monCount) + 1
	local  posIdx = System.getRandomNumber(posCount) + 1
	local  monCfg = cfg.Monsters[monIdx]
	local  posCfg = cfg.MonsterPos[posIdx]
	if  not monCfg or not posCfg  then
	    return 
	end
	local  dVar = System.getDyanmicVar()
	local  hScene = Fuben.getSceneHandleById(posCfg[1], 0)
	if hScene then
		local  monster = Fuben.createMonster(hScene,monCfg.monId,posCfg[2],posCfg[3],monCfg.livetime)
		if monster == nil then
		    return
		end
		if not dVar.SecretTreasureMonIdx then
			dVar.SecretTreasureMonIdx = {}
		end
		dVar.SecretTreasureMonIdx[idx] = monCfg.monId
	end  
end
	
--活动结束
function SecretTreasureEnd()	-- @DebugLua 1003 13 2
	local sysVar = System.getDyanmicVar()
	if not  sysVar.SecretTreasureStart then
		return
	end
	SecretTreasureRoundEndClearMonsters()
	SecretTreasureAwardAll()

	SecretTreasureEndHandler()
	Actor.unregScriptCallback(System.getGlobalNpc(), "OnSecretTreasureExpAdd")
end

function SecretTreasureEndHandler()

	System.broadcastTipmsg(Lang.ScriptTips.SecretTreasure015,ttScreenCenter + ttChatWindow)
	Actor.unregScriptCallback( System.getGlobalNpc(), "SecretTreasurePreSecondTimeFunc")
	local sysVar = System.getDyanmicVar()
	sysVar.SecretTreasureStart = nil
	sysVar.SecretTreasureActorTime = nil
	sysVar.SecretTreasureActorId = nil
	sysVar.SecretTreasureRefeshIdx = nil
	sysVar.SecretTreasureMonIdx = nil
	sysVar.SecretTreasureItemMonIdx = nil
	sysVar.SecretTreasureRoundEndTime = nil
	sysVar.SecretTreasureItemMonItemId = nil
	local nSceneId = SecretTreasureCfg.EnterPos[1][1]
	local playerList = LuaHelp.getSceneActorListById(nSceneId)  --获取在线玩家对象列表	
	if(playerList == nil)then
		return
	end
	for k,v in pairs(playerList)do	--踢出场景
		Actor.enterScene(v,unpack(SecretTreasureCfg.ExitPos))	
		CloseActivityRightPanel(v)			--关闭活动面板
	end
end

function SecretTreasureLeaveActivityHandler(sysarg)
	CloseActivityRightPanel(sysarg)	

	Actor.delBuffById( sysarg, SecretTreasureCfg.changeBuff )

	local  dVar = System.getDyanmicVar()
	if not dVar.SecretTreasureActorId then
		return
	end
	for idx = 1, SecretTreasureCfg.EveryMonsterCount do
		if dVar.SecretTreasureActorId[idx] and dVar.SecretTreasureActorId[idx] == Actor.getActorId(sysarg) then
			--[[
			local actorId = Actor.getActorId(sysarg)
			local itemId = gRankMgr_GetValueById(actorId, RANK_DEFINE_SECRET_TREASURE, idx)
			gRankMgr_SetRankEx(actorId, RANK_DEFINE_SECRET_TREASURE, idx, 0)
			gRankMgr_AddValue(actorId, RANK_DEFINE_SECRET_TREASURE, -1)
			]]
			--[[
			local PropValue = Actor.getUInt64Property(sysarg, PROP_ACTOR_HEAD_ITEM)
			local itemId = System.getInt64UShort(PropValue, idx - 1)
			PropValue = System.setInt64UShort(PropValue, idx - 1, 0)
			Actor.setUInt64Property(sysarg, PROP_ACTOR_HEAD_ITEM, PropValue)
			]]
			local itemId = GetPropertyHeadItemValue(sysarg, idx)
			SetPropertyHeadItemValue(sysarg, idx, 0)
			
			dVar.SecretTreasureActorId[idx] = nil
			dVar.SecretTreasureActorTime[idx] = nil
			SecretTreasureCreateDropItemMon(sysarg, idx, itemId)
		end
	end
end

--玩家下线
function SecretTreasureLogoutHandler(sysarg)
	SecretTreasureLeaveActivity(sysarg)
end

--离开活动场景
function SecretTreasureLeaveActivity(sysarg)
	Actor.enterScene(sysarg,unpack(SecretTreasureCfg.ExitPos))	
end

--玩家丢掉物品，刷掉落怪
function  SecretTreasureCreateDropItemMon(sysarg, idx, itemId)
	local  cfg = SecretTreasureCfg
    local  monCount = table.getn(cfg.DropItemMonsters)
	local  monIdx = System.getRandomNumber(monCount) + 1
	local  monCfg = cfg.DropItemMonsters[monIdx]
	if  not monCfg  then
	    return 
	end
	local x, y
	local sceneId = Actor.getSceneId(sysarg)
	if sceneId ~= SecretTreasureCfg.EnterPos[1][1] then
		local  posCount = table.getn(cfg.MonsterPos)
		local  posIdx = System.getRandomNumber(posCount) + 1
		local  posCfg = cfg.MonsterPos[posIdx]
		if not posCfg  then
		    return 
		end
		x, y = posCfg[2], posCfg[3]
	else
		x, y = Actor.getPosition(sysarg,0,0)
	end
	
	local  dVar = System.getDyanmicVar()
	local  hScene = Fuben.getSceneHandleById(SecretTreasureCfg.EnterPos[1][1], 0)
	if hScene then
		local  monsterNum = Fuben.createMonstersInRange(hScene,monCfg.monId,x-cfg.DropItemRanPos[1],y-cfg.DropItemRanPos[2],x+cfg.DropItemRanPos[1],y+cfg.DropItemRanPos[2],1,monCfg.livetime)
		if monsterNum == 0 then
		    return
		end
		if not dVar.SecretTreasureItemMonIdx then
			dVar.SecretTreasureItemMonIdx = {}
		end
		if not dVar.SecretTreasureItemMonItemId then
			dVar.SecretTreasureItemMonItemId = {}
		end
		dVar.SecretTreasureItemMonIdx[idx] = monCfg.monId
		dVar.SecretTreasureItemMonItemId[idx] = itemId
		
	   	SecretTreasureRightPanelDataAll()
	end  
end

--玩家捡起物品
function SecretTreasurePickUpItem(monster, sysarg, monId)
	local  dVar = System.getDyanmicVar()
	if not dVar.SecretTreasureItemMonIdx then
		return
	end
	if not dVar.SecretTreasureItemMonItemId then
		return
	end	
	local state = false
	local itemId
	local monIdx
	for idx = 1, SecretTreasureCfg.EveryMonsterCount do
		if dVar.SecretTreasureItemMonIdx[idx] and dVar.SecretTreasureItemMonIdx[idx] == monId and dVar.SecretTreasureItemMonItemId[idx] then
			monIdx = idx
			itemId = dVar.SecretTreasureItemMonItemId[idx]
			state = true
			break
		end
	end
	if state == false then
		return
	end
	

	dVar.SecretTreasureItemMonItemId[monIdx] = nil
	dVar.SecretTreasureItemMonIdx[monIdx] = nil

	--[[
	local actorId = Actor.getActorId(sysarg)
	gRankMgr_SetRankEx(actorId, RANK_DEFINE_SECRET_TREASURE, monIdx, itemId)
	gRankMgr_AddValue(actorId, RANK_DEFINE_SECRET_TREASURE, 1)
	]]
	--[[
	local PropValue = Actor.getUInt64Property(sysarg, PROP_ACTOR_HEAD_ITEM)
	PropValue = System.setInt64UShort(PropValue, monIdx - 1, itemId)
	Actor.setUInt64Property(sysarg, PROP_ACTOR_HEAD_ITEM, PropValue)
	]]
	SetPropertyHeadItemValue(sysarg, monIdx, itemId)

	if not dVar.SecretTreasureActorId then
		dVar.SecretTreasureActorId = {}
	end
	dVar.SecretTreasureActorId[monIdx] = Actor.getActorId(sysarg)
	if not dVar.SecretTreasureActorTime then
		dVar.SecretTreasureActorTime = {}
	end
	dVar.SecretTreasureActorTime[monIdx] = System.getCurrMiniTime() + SecretTreasureCfg.LastTime

	--local playerName = Actor.getName(sysarg)
	local playerName = Lang.ScriptTips.SecretTreasure017

	local nSceneId = SecretTreasureCfg.EnterPos[1][1]
	local hScene = Fuben.getSceneHandleById(nSceneId, 0)
	Fuben.sendMsgToScene(hScene,string.format(Lang.ScriptTips.SecretTreasure011, playerName), ttScreenCenter + ttChatWindow)
	SecretTreasureRightPanelDataAll()
end

--场景玩家更新活动界面
function SecretTreasureRightPanelData(sysarg)

	local panelInfo = {}
	panelInfo.sts = 1    			--状态,1-进行中，2-已结束
	panelInfo.title = Lang.ScriptTips.SecretTreasure003

	local sysVar = System.getDyanmicVar()
	panelInfo.timeTitle = Lang.ScriptTips.SecretTreasure004
	panelInfo.restTime = sysVar.SecretTreasureStart + SecretTreasureCfg.ActivityTime - System.getCurrMiniTime()

	panelInfo.contentTitle = Lang.ScriptTips.SecretTreasure005
	panelInfo.contents = {}
	if not sysVar.SecretTreasureActorId then
		sysVar.SecretTreasureActorId = {}
	end
	local data
	for idx = 1, SecretTreasureCfg.EveryMonsterCount do
		if sysVar.SecretTreasureActorId[idx] and sysVar.SecretTreasureActorId[idx] == -1 then
			data = Lang.ScriptTips.SecretTreasure009
		elseif sysVar.SecretTreasureActorId[idx] then
			local player = Actor.getActorById(sysVar.SecretTreasureActorId[idx])
			if player then
				--data = Actor.getName(player)
				data = Lang.ScriptTips.SecretTreasure017
			else
				data = Lang.ScriptTips.SecretTreasure008
			end
		else
			data = Lang.ScriptTips.SecretTreasure008
		end
		table.insert(panelInfo.contents, data)
	end

	panelInfo.awardTitle = ""
	panelInfo.awards = {}

	panelInfo.buttons = {}	--1：退出副本，2-单倍领奖，3-双倍领奖
	table.insert(panelInfo.buttons, 1)
 
	local panelType = 2
	local activityId = enRightPanelActivity_SecretTreasure
	panelInfo.subActivityId = SecretTreasureCfg.EnterPos[1][1]
	OpenActivityRightPanel(sysarg, panelType, activityId, panelInfo)

	SecretTreasureSendPlayerTrueasureData(sysarg)
end

--下发玩家头顶item信息
function SecretTreasureSendPlayerTrueasureData(sysarg)
	--[[
	local rankPtr = Ranking.getRanking(RankList[RANK_DEFINE_SECRET_TREASURE].strName)
	if not rankPtr then
		return
	end
	local rankCount = Ranking.getRankItemCount(rankPtr)
	if rankCount == 0 then
		return
	end

	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sGetSecretTreasureInfo)
	
	if not netPack then
		return
	end
	local count = rankCount
	if count > SecretTreasureCfg.EveryMonsterCount then
		count = SecretTreasureCfg.EveryMonsterCount 
	end
	local state = true
	for idx = count, 1, -1 do
		local item = Ranking.getItemFromIndex(rankPtr, idx-1)
		if item then
			local point = tonumber(Ranking.getPoint(item))
			if point > 0 then
				if state then
					DataPack.writeByte(netPack, idx)
					state = false
				end
				local actorId = tonumber(Ranking.getId(item))
				local pActor = Actor.getActorById(actorId)
				local hActor = Actor.getHandle(pActor)
				DataPack.writeUint64(netPack, hActor)
				for ii = 1, SecretTreasureCfg.EveryMonsterCount do
					local itemId = gRankMgr_GetValueById(actorId, RANK_DEFINE_SECRET_TREASURE, ii)
					DataPack.writeUInt(netPack, itemId)
				end
			end
		end
	end
	if state then
		DataPack.writeByte(netPack, 0)
	end
	DataPack.flush(netPack)

	]]
end


--场景玩家更新活动界面
function SecretTreasureRightPanelDataAll()
	local nSceneId = SecretTreasureCfg.EnterPos[1][1]
	local playerList = LuaHelp.getSceneActorListById(nSceneId)  --获取在线玩家对象列表	
	if(playerList == nil)then
		return
	end
    for i, player in ipairs(playerList) do
        SecretTreasureRightPanelData(player)
    end
end

--没杀人者
function SecretTreasureActorDeathWithNoKiller( sysarg )
	--BaseFuc_Print("Enter SecretTreasureActorDeath")
	if sysarg == nil then
		return
	end
	if SecretTreasureCfg.EnterPos[1][1] ~= Actor.getSceneId(sysarg) then			--不是在场景死的	
		return
	end

	local  dVar = System.getDyanmicVar()
	if not dVar.SecretTreasureActorId then
		dVar.SecretTreasureActorId = {}
	end
	if not dVar.SecretTreasureActorTime then
		dVar.SecretTreasureActorTime = {}
	end

	for idx = 1, SecretTreasureCfg.EveryMonsterCount do
		if dVar.SecretTreasureActorId[idx] == Actor.getActorId(sysarg) then
			local itemId = GetPropertyHeadItemValue(sysarg, idx)
			SetPropertyHeadItemValue(sysarg, idx, 0)
			dVar.SecretTreasureActorId[idx] = nil
			dVar.SecretTreasureActorTime[idx] = nil

			SecretTreasureCreateDropItemMon(sysarg, idx, itemId)
		end
	end
end


--杀人加分 sysarg = 被杀者
function SecretTreasureActorDeath( sysarg, killerPtr )
	--BaseFuc_Print("Enter SecretTreasureActorDeath")
	if sysarg == nil then
		return
	end
	if SecretTreasureCfg.EnterPos[1][1] ~= Actor.getSceneId(sysarg) then			--不是在场景死的	
		return
	end

	local  dVar = System.getDyanmicVar()
	if not dVar.SecretTreasureActorId then
		dVar.SecretTreasureActorId = {}
	end
	if not dVar.SecretTreasureActorTime then
		dVar.SecretTreasureActorTime = {}
	end

	if killerPtr == nil or Actor.isMonster(killerPtr) or Actor.isDeath(killerPtr) or SecretTreasureCfg.EnterPos[1][1] ~= Actor.getSceneId(killerPtr) then
		for idx = 1, SecretTreasureCfg.EveryMonsterCount do
			if dVar.SecretTreasureActorId[idx] == Actor.getActorId(sysarg) then
				local itemId = GetPropertyHeadItemValue(sysarg, idx)
				SetPropertyHeadItemValue(sysarg, idx, 0)
				dVar.SecretTreasureActorId[idx] = nil
				dVar.SecretTreasureActorTime[idx] = nil

				SecretTreasureCreateDropItemMon(sysarg, idx, itemId)
			end
		end
	else
		--[[
		local actorId = Actor.getActorId(sysarg)
		local killerId = Actor.getActorId(killerPtr)

		for idx = 1, #SecretTreasureCfg.EveryMonsterCount do
			local itemId = gRankMgr_GetValueById(actorId, RANK_DEFINE_SECRET_TREASURE, idx)
			if itemId and itemId ~= 0 then
				gRankMgr_SetRankEx(actorId, RANK_DEFINE_SECRET_TREASURE, idx, 0)
				gRankMgr_SetRankEx(killerId, RANK_DEFINE_SECRET_TREASURE, idx, itemId)
				gRankMgr_AddValue(actorId, RANK_DEFINE_SECRET_TREASURE, -1)
				gRankMgr_AddValue(killerId, RANK_DEFINE_SECRET_TREASURE, 1)
			end
		end
		]]
		
		--[[
		local PropValue = Actor.getUInt64Property(sysarg, PROP_ACTOR_HEAD_ITEM)
		Actor.setUInt64Property(sysarg, PROP_ACTOR_HEAD_ITEM, 0)
		Actor.setUInt64Property(killerPtr, PROP_ACTOR_HEAD_ITEM, PropValue)
		]]

		--[[  --不能这么处理 
		local PropValue = Actor.getUIntProperty(sysarg, PROP_ACTOR_HEAD_ITEM1)
		Actor.setUIntProperty(sysarg, PROP_ACTOR_HEAD_ITEM1, 0)
		Actor.setUIntProperty(killerPtr, PROP_ACTOR_HEAD_ITEM1, PropValue)
		local PropValue2 = Actor.getUIntProperty(sysarg, PROP_ACTOR_HEAD_ITEM2)
		Actor.setUIntProperty(sysarg, PROP_ACTOR_HEAD_ITEM2, 0)
		Actor.setUIntProperty(killerPtr, PROP_ACTOR_HEAD_ITEM2, PropValue2)
		]]

		

		local BroadCastState = false

		for idx = 1, SecretTreasureCfg.EveryMonsterCount do
			if dVar.SecretTreasureActorId[idx] == Actor.getActorId(sysarg) then
				BroadCastState = true
				local itemId = GetPropertyHeadItemValue(sysarg, idx)
				SetPropertyHeadItemValue(sysarg, idx, 0)
				SetPropertyHeadItemValue(killerPtr, idx, itemId)
				dVar.SecretTreasureActorId[idx] = Actor.getActorId(killerPtr)
				dVar.SecretTreasureActorTime[idx] = System.getCurrMiniTime() + SecretTreasureCfg.LastTime
			end
		end

		if BroadCastState then
			--local playerName1 = Actor.getName(killerPtr)
			--local playerName2 = Actor.getName(sysarg)

			local playerName1 = Lang.ScriptTips.SecretTreasure017
			local playerName2 = Lang.ScriptTips.SecretTreasure017


			local nSceneId = SecretTreasureCfg.EnterPos[1][1]
			local hScene = Fuben.getSceneHandleById(nSceneId, 0)
			Fuben.sendMsgToScene(hScene,string.format(Lang.ScriptTips.SecretTreasure012, playerName1, playerName2), ttScreenCenter + ttChatWindow)

			SecretTreasureRightPanelDataAll()
		end
	end
end

--发送回合所有奖励
function SecretTreasureAwardAll()
	for idx = 1,SecretTreasureCfg.EveryMonsterCount do
		SecretTreasureAward(idx)
	end
end

--发送奖励
function SecretTreasureAward(idx)
	local  dVar = System.getDyanmicVar()
	if not dVar.SecretTreasureActorId then
		return
	end
	if not dVar.SecretTreasureActorId[idx] then
		return
	end
	local sysarg = Actor.getActorById(dVar.SecretTreasureActorId[idx])
	if not sysarg then
		return
	end
	if not dVar.SecretTreasureActorId then
		dVar.SecretTreasureActorId = {}
	end
	if not dVar.SecretTreasureActorTime then
		dVar.SecretTreasureActorTime = {}
	end
	dVar.SecretTreasureActorId[idx] = -1
	dVar.SecretTreasureActorTime[idx] = nil

	--[[
	local PropValue = Actor.getUInt64Property(sysarg, PROP_ACTOR_HEAD_ITEM)
	local itemId = System.getInt64UShort(PropValue, idx - 1)
	PropValue = System.setInt64UShort(PropValue, idx - 1, 0)
	Actor.setUInt64Property(sysarg, PROP_ACTOR_HEAD_ITEM, PropValue)
	]]
	local itemId = GetPropertyHeadItemValue(sysarg, idx)
	SetPropertyHeadItemValue(sysarg, idx, 0)

	--[[
	local actorId = Actor.getActorId(sysarg)

	local itemId = gRankMgr_GetValueById(actorId, RANK_DEFINE_SECRET_TREASURE, idx)
	gRankMgr_SetRankEx(actorId, RANK_DEFINE_SECRET_TREASURE, idx, 0)
	gRankMgr_AddValue(actorId, RANK_DEFINE_SECRET_TREASURE, -1)
	]]

	local ItemAwards = {{type = 0, id = itemId, count = 1, bind = 0}}
	if IsBagGridEnough(sysarg, ItemAwards) then
		GiveCommonAward(sysarg, ItemAwards, LogIds.SecretTreasureLogId, Lang.LogTips.SecretTreasure)
	else	
		local actorId = Actor.getActorId(sysarg)
		local title = Lang.ScriptTips.SecretTreasure001
		local content = Lang.ScriptTips.SecretTreasure002
		Awards.GiveToMailById(actorId, ItemAwards, title, content, LogIds.SecretTreasureLogId, Lang.LogTips.SecretTreasure)
	end	
	--local itemName = Item.getItemName(itemId)
	local itemStr = Item.getItemLinkMsg(itemId)
	--local playerName = Actor.getName(sysarg)
	local playerName = Lang.ScriptTips.SecretTreasure017
	System.broadcastTipmsg(string.format(Lang.ScriptTips.SecretTreasure013, playerName, itemStr), ttScreenCenter + ttChatWindow)
end

--活动判断
function IsSecretTreasure(sysarg)
	local mySceneId =  Actor.getSceneId(sysarg)
	if(mySceneId == SecretTreasureCfg.EnterPos[1][1])then
		return true
	end
	return false
end



--根据第几波第几天获取奖励
function GetSecretTreasureRewardItem()
	local  dVar = System.getDyanmicVar()
	local openDays = System.getDaysSinceOpenServer()
	if not dVar.SecretTreasureRefeshIdx then
		return 0
	end
	local cfg = SecretTreasureCfg.DropItems[dVar.SecretTreasureRefeshIdx]
	if not cfg then
		return 0
	end
	local openDaysCfg = GetElemByCond(cfg, openDays)
	if not openDaysCfg then
		return 0
	end
	local dropsCfg = openDaysCfg.drop
	local idx, item = GetItemIdxRand(dropsCfg)
	return item.id
end
